package com.my.recyclerandrecycler.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.my.recyclerandrecycler.R;
import com.my.recyclerandrecycler.bean.ShoppingBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：RecyclerAndRecycler
 * 创建人：mwb
 * 创建时间：2018/8/16 15:51
 */
public class MainAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<ShoppingBean.DataBean> data;
    private ChildAdapter childAdapter;
    private List<CheckBox> checkBoxes = new ArrayList<CheckBox>();
    private boolean selectAll;
    private double dPrice = 0;

    public MainAdapter(Context context, List<ShoppingBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    public void setSelet(boolean selectAll) {
        this.selectAll = selectAll;
        childAdapter.setAllCheckBox(checkBoxes, selectAll);

        // 1.获取所有商品的数量 TODO===============
        for (int i = 0; i < data.size(); i++) {
            for (int a = 0; a < data.get(i).getList().size(); a++) {
                Log.i("mwb", "数量：" + data.get(i).getList().get(a).getGoods_num());

                dPrice += data.get(i).getList().get(a).getGoods_num() * data.get(i).getList().get(a).getGoods_price();
            }
            Log.i("mwb", "--------");
        }

        Log.i("mwb", "总价：" + dPrice);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_main, null);
        MainHolder holder = new MainHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MainHolder) {
            ((MainHolder) holder).tvShop.setText(data.get(position).getStore_name());

            childAdapter = new ChildAdapter(context, data.get(position).getList(), new ChildAdapter.OnCheckBoxReturnListener() {
                @Override
                public void onCheckBox(CheckBox checkBox) {
                    checkBoxes.add(checkBox);
                    if (selectAll) {
                        setSelet(selectAll);
                    }
                }

                @Override
                public void addNum(int position) {
                    // TODO
                }

                @Override
                public void cutNum(int position) {
                    // TODO
                }

                @Override
                public void currentPrice(double price) {
                    // TODO
                    Log.i("mwb", "当前的价格：" + price);
                }
            });
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 1, GridLayoutManager.VERTICAL, false);

            ((MainHolder) holder).recyclerView.setLayoutManager(gridLayoutManager);
            ((MainHolder) holder).recyclerView.setAdapter(childAdapter);

        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    private class MainHolder extends RecyclerView.ViewHolder {
        private TextView tvShop;
        private RecyclerView recyclerView;

        public MainHolder(View itemView) {
            super(itemView);

            tvShop = itemView.findViewById(R.id.item_main_shop_tv);
            recyclerView = itemView.findViewById(R.id.item_recycler_main);
        }
    }
}
